// console.log(userInfo._id);
//获取全部错题数据
let errArr;
let currentPage = 0;

//获取所有试题
function getAllError() {
  $.ajax({
    url: "/errors/getErrors",
    type: "post",
    data: { studentId: userInfo._id },
    success(res) {
      errArr = res.result || [];
    }
  });
}

//渲染错题
renderErr();
function renderErr() {
  getAllError();

  // 渲染错题总数量
  $("#errNum").text(errArr.length);
  // 数据库没有数据直接返回
  if (errArr.length == 0) return;

  //渲染错题到页面
  errArr.forEach(forCall);

  // 让第一个显示
  $(`.exe0`).show();

  //删除错题
  $(".deleteErr").on("click", function () {
    let _id = $(this).data("delete");
    $.ajax({
      url: "/errors/deleteErrors",
      type: "post",
      data: { _id },
      success(res) {
        console.log(res);
        $("#exe").html("");
        renderErr();
      }
    });
  });

  //收藏错题
  $(".collectErr").on("click", function () {
    let exerciseId = $(this).data("collect");
    let studentId = userInfo._id;
    $.ajax({
      url: "/collections/createCollections",
      type: "post",
      data: { studentId, exerciseId },
      success(res) {
        console.log(res);
        $("#collectContent").html("");
        renderCollect();
      }
    });
  });

  // 上一题
  $(".preExe").on("click", function () {
    if (currentPage <= 0) return alert("前面没有了");
    currentPage--;
    $(`.exe${currentPage}`).show().siblings().hide();
    // console.log(currentPage);
  });

  // 下一题
  $(".nextExe").on("click", function () {
    if (currentPage >= errArr.length - 1) return alert("已经到底了");
    currentPage++;
    $(`.exe${currentPage}`).show().siblings().hide();
    // console.log(currentPage);
  });
}

//渲染错题的foreach回调函数
function forCall(v, index) {
  let exeInfo = v.exerciseId;
  let ansArr = ["A", "B", "C", "D", "未选"];
  $("#exe").append(`
           <div class='exe${index}' style='display:none'>
           <h4>${index + 1}.${exeInfo.topics}</h4>
           <ul class="main2">
             <li>
               <button class="btn btn-light">A</button>
               <span>${exeInfo.options[0]}</span>
             </li>
             <li>
               <button class="btn btn-light">B</button>
               <span>${exeInfo.options[1]}</span>
             </li>
             <li>
               <button class="btn btn-light">C</button>
               <span>${exeInfo.options[2]}</span>
             </li>
             <li>
               <button class="btn btn-light">D</button>
               <span>${exeInfo.options[3]}</span>
             </li>
           </ul>
           <div class="button-box">
             <button class="btn btn btn-primary collectErr" data-collect='${
               exeInfo._id
             }'>收藏</button>
             <button class="btn btn btn-primary deleteErr" data-delete='${
               v._id
             }'>标记为已学会</button>
             <div>
               <button class="btn btn btn-primary preExe" id=''>上一题</button>
               <button class="btn btn btn-primary nextExe" data-container="body" data-toggle="popover" data-placement="right" data-content="Right popover">下一题</button>
             </div>
           </div>
           <div class="footer1">
             <h6 class="aaa" style='color:red'>
               考生答案：${ansArr[v.errorAnswer]}
               <button type="button" class="btn btn-danger">错误</button>
             </h6>
             <h6>正确答案：${ansArr[exeInfo.answer]}</h6>
             <p>解析：${exeInfo.analysis}</p>
           </div>
           </div>
`);
}
